package com.noble.meether.chat.ui.agora.act.video;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.socket.NetData;
import com.noble.glamour.module.logic.socket.listener.SendCallBack;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.utils.InputUtils;
import com.noble.library.utils.NoDoubleClickListener;
import com.noble.library.utils.UIUtil;
import com.noble.library.view.base.BasePanel;
import com.noble.meether.R;

/**
 * 音视频输入框panel
 * Created by Su on 2017/6/28.
 */

public class RtcChatInputPanel extends BasePanel {
    private EditText chat_edit;

    public RtcChatInputPanel(Context context) {
        super(context);
        setContentView(R.layout.f1_chat_input_panel);

        initView();
    }

    private void initView() {
        chat_edit = (EditText) findViewById(R.id.chat_edit);
        findViewById(R.id.chat_send).setOnClickListener(listener);
        findViewById(R.id.chat_input_close).setOnClickListener(listener);
        UIUtil.endCursor(chat_edit);
    }

    private void send() {
        final String text = chat_edit.getText().toString().trim();
        if (TextUtils.isEmpty(text.trim())) {
            PToast.showShort("发送内容不能为空");
            return;
        }
        // 发送文本消息
        ModuleMgr.getChatMgr().sendTextMsg(ModuleMgr.getRtcEnginMgr().getEngineConfig().mOtherId, text, false, new SendCallBack() {
            @Override
            public void onResult(long msgId, long sender, String contents) {
                MsgMgr.getInstance().sendMsg(MsgType.MT_Msg_AvChat_MY, text);
            }

            @Override
            public void onSendFailed(NetData data) {
                sendMsgFail();
            }
        });
        inputPanelListener.onInputbtnClick(null);
        refresh(View.GONE);
    }

    private void sendMsgFail() {
        getActivity().runOnUiThread(() -> PToast.showShort(App.context.getString(R.string.send_error_try_again)));
    }

    public void refresh(int visible) {
        chat_edit.setText("");
        UIUtil.endCursor(chat_edit);
        if (visible == View.GONE) {
            InputUtils.forceClose((Activity) getContext());
            return;
        }
        InputUtils.forceOpen(getContext());
    }

    private NoDoubleClickListener listener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.chat_send:
                    send();
                    break;

                case R.id.chat_input_close:
                    refresh(View.GONE);

                    if (inputPanelListener == null) return;
                    inputPanelListener.onInputbtnClick(v);
                    break;
            }
        }
    };

    private InputPanelListener inputPanelListener;

    public void setInputPanelListener(InputPanelListener inputPanelListener) {
        this.inputPanelListener = inputPanelListener;
    }

    public interface InputPanelListener {
        void onInputbtnClick(View v);
    }
}